﻿using System;
using PSLibrary = Microsoft.Office.Project.Server.Library;

namespace FluentPS.Impersonation
{
    public class PsiImpersonationContext: IDisposable
    {
        public bool IsWindowsAccount;
        public Guid TrackingGuid = Guid.NewGuid();
        public string Lcid = "1033";
        public string UserNTAccount;
        public Guid ResourceGuid;
        public Guid SiteId;

        public IImpersonation _impersonationHandler;

        public PsiImpersonationContext(IImpersonation impersonationHandler)
        {
            _impersonationHandler = impersonationHandler;
        }

        public string GetImpersonationHeader()
        {
            var contextInfo = new PSLibrary.PSContextInfo(IsWindowsAccount, UserNTAccount, ResourceGuid, TrackingGuid, SiteId, Lcid);
            return PSLibrary.PSContextInfo.SerializeToString(contextInfo);
        }

        public void Dispose()
        {
            if (_impersonationHandler != null)
            {
                _impersonationHandler.RemoveContext(this);
            }
        }
    }
}
